perm filename NOTES.IMB[LSP,JRA]2 blob sn#255919 filedate 1976-12-31 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	.SEC(Imperative constructs in LISP,,,P274:)
C00012 ENDMK
C⊗;
.SEC(Imperative constructs in LISP,,,P274:)
.SS(Introduction,,P273:)
All of the language constructs we have introduced 
so far are based on a computational interpretation of function
application.  We therefore call the language, applicative LISP⊗↓It is
also referred to as "pure LISP".←.
Though the applicative subset of LISP is rich and powerful, it is
often conveinent to  have access to another type of language
constuct called an %2imperative%1. 

An imperative construct has the intent of a command.
For that reason imperative commands are called statements rather than expressions
since it is the %2effect%1 of the command which
is important; and its %2value%1, if it has one, is  of secondary 
importance⊗↓Some programming languages insist that statements do %2not%1
have value. The imperatives of LISP %2do%1 have values; they may be
used or ignored as the programmer desires.←.
For example, most programming languages have sequencing commands: "first
do %3S%41%1, then do %3S%42%1"; that might be written "%3S%41%3;#S%42%1",
where %3S%41%1 and %3S%42%1 are statements.
The nature of imperatives is somewhat illusory, since sequencing can be
expressed in our applicative subset:

.BEGIN CENTER;SELECT 3;

S%41%3;S%42%1### is### %3λ[[] S%42%3][S%41%3]
.END

Here we depend on LISP's call by value evaluation.
Since %3S%41%1 is the argument, it is evaluated first; 
then %3S%42%1 is evaluated. The value of the sequence is the value of %3S%42%1.
LISP calls this sequencing operation %3prog%42%1.

An area  which is thought to be the  province of imperatives is
that of the assignment statement. We will discuss assignment statements further
in {yonss(P37)}, but the intent of such a statement, written:

.BEGIN CENTER;TURN OFF "←";

<identifier> ← <expression>
.END
is to think of the <identifier> as a "box" and the intent of the
assignment is to put the value of the <expression> in the "box".
Yet, function application can encompass many of the traditional uses
of assignment. 
.GROUP;
Recall our definitions of %3length%1 and %3length%41%1:

.BEGIN CENTERIT;SELECT 3;group;
.P272:

←length[l] <= [null[l] → 0; %et%* → add1[length[rest[l]]]]

←length%41%*[l] <= length%9'%*[l;0]

←length%9'%*[l1;c] <= [null[l1] → c; %et%* → length%9'%*[rest[l1];add1[c]]]
.END
.APART;
The variable %3c%1 is being used as a "box" or "accumulator" ⊗↑[Moor#74]↑
to accumulate length of the list.
We will show in {yonss(P37)} that
%3length%41%1 can be translated into a
program using several imperative features: statements, sequencing, assignments,
and an imperative control regime called %2iteration%1.
We will study iterative control in {yonss(P37)} and {yonss(P253)}
but  it can be shown that iterative control can be
translated into recursive control#(⊗↑[McC#60]↑,#⊗↑[Sam#75]↑).


In many instances the most important implication of imperatives
 is "convenience". There are algorithms which are most
naturally described  in terms of sequences of statements and iteration;
and there are many lessons to be learned by careful analysis of
natural implementations of imperative constructs. We are not looking for
a minimal language, we are looking for a  useful programming tool.
One of the most useful places for imperative constructs is in
area of non-local variables, using assignment statements to pass information
across applicative boundaries. This technique is called  using 
side-effects. It may very well be that the most distinctive
features of imperative constructs are involved with their
side-effect aspects. 

For example, 
LISP implementations include a unary primitive named %3print%1 whose effect is to
print the value of its argument on the current output device. This
function also returns its evaluated argument as the value of the %3print%1
statement. Thus mathematically, %3print%1 acts like an identity function,
but its execution certainly affects the programmer's 
environment⊗↓Whether 
the act of printing is a side-effect or the fact that %3print%1
returns a value is a side-effect depends on your point of view.←.

Side-effects can also spoil some of the  theoretical properties of
languages.
In our earlier discussions, we have implied that call-by-value is a
subset of call-by-name in the sense that whenever a call-by-value
computation  terminates, the corresponding call-by-name computation
will as well. In the presence of side-effects, this is %2not%1 true.
We give an example on {yon(P278)}.